import { get } from "./request.js";
import { v4 as uuidv4 } from 'uuid';

let host = '';
let signature = '';
let ossAccessKeyId = '';
let policy = '';
let key = '';
let securityToken = ''; 
let filePath = ''; // 待上传文件的文件路径。
var now = Date.parse(new Date()) / 1000; 
var expire = '';
let signUrl='/common/uploadSign'

export async function h5Upload (filePath,imageSuffixes) {
	let obj=null;
	await get(signUrl).then(res=>{
		obj=res.data;
	});
	return new Promise((resolve, reject) => {
		
		obj['ossAccessKeyId'] = obj['accessid'];
		obj['expire'] = parseInt(obj['expire']);
		
		if(!imageSuffixes) {
			console.log("上传失败,不支持该文件上传")
			return
		}
		console.log("正在上传")
		let suffix ='.' + imageSuffixes;
		obj['key'] = obj['dir'] + new Date().getTime() + Math.floor(Math.random() * 100000) + suffix
		uni.uploadFile({
			url: obj['host'], 
			filePath: filePath,
			name: 'file',
			formData: obj,
			success: (uploadFileRes) => {
				resolve('https://seqbs.oss-cn-beijing.aliyuncs.com/'+obj['key'])
			},
			fail(err) {
				reject(err)
			}
		});
	});
} 